# coding: utf-8
from django.db import transaction
from django.http import HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
from common.overwrites import z_render_to_response
from common.models.position import JPositionCounty, JPositionTown, JPositionVillage
from django.conf import settings
from django.http import JsonResponse


def get_county_list(request):
    cs = JPositionCounty.objects.filter(city_id=settings.XINYU_CITY_ID)

    county_list = []
    for c in cs:
        county_list.append({'t_county_id': c.county_id,
                            'name': c.county_name})
    return JsonResponse({'county_list': county_list})


@csrf_exempt
def get_town_list(request):
    if request.method == 'POST':
        ts = JPositionTown.objects.filter(county_id=request.POST.get('county_id'))

        town_list = []
        for t in ts:
            town_list.append({'town_id': t.town_id,
                              'name': t.town_name})
        return JsonResponse({'town_list': town_list})


@csrf_exempt
def get_village_list(request):
    if request.method == 'POST':
        vs = JPositionVillage.objects.filter(town_id=request.POST.get('town_id'))

        village_list = []
        for v in vs:
            village_list.append({'village_id': v.village_id,
                                 'name': v.village_name})
        return JsonResponse({'village_list': village_list})